#ifndef COMMON_XCONFIG_H_
#define COMMON_XCONFIG_H_

#include "Def.h"

#include <string>
#include <fstream>
using std::string;
using std::ofstream;
using std::ifstream;
using std::endl;
using std::ios;

static void trim(string &str){
    int i = 0, j = str.size()-1;
	while(str[i] == ' ' || str[i] == '\t') i++;
	while(str[j] == ' ' || str[j] == '\t') j--;
	str = str.substr(i, j-i+1);
}

bool config_init(const char *path, Config &config){
	std::ifstream cfg;
	cfg.open(path);
	if(!cfg.is_open()){
		return false;
	}

	std::string line;
	while(cfg.good()){
		std::getline(cfg, line);
		int cur = line.find("=") + 1;
		if(cur < 0) continue;

		line.append(",");
		if(line.find("port") == 0){
			std::string item;
			int next = cur;
			while((cur = line.find(",", next)) >= 0) {
				item = line.substr(next, cur-next);
				port_t port = (port_t)atoi(item.c_str());
				config.port.push_back(port);
				next = cur+1;
			}
		} else if(line.find("ignore_process") == 0){
			std::string item;
			int next = cur;
			while((cur = line.find(",", next)) >= 0) {
				item = line.substr(next, cur-next);
				trim(item);
				config.ignore_proc.push_back(item);
				next = cur+1;
			}
		} else if(line.find("proxy_addr") == 0){
			std::string addr = line.substr(cur);
			addr = addr.substr(0, addr.size()-1);
			trim(addr);
			config.proxy_addr = addr;
		}
		else {

		}
	}
	return true;
}
#endif